/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrt;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Iterator;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;

import com.atjava.jcrt.core.ItemType;
import com.atjava.jcrt.core.transaction.XmlJcrtInfoDetail;
import com.atjava.jcrt.core.transaction.XmlServices;
import com.atjava.jcrt.core.transaction.XmlTransactionException;
import com.atjava.jcrt.utility.DependHelper;
import com.atjava.jcrt.utility.UtilHelper;
/**
 * Base info
 * 
 * @author biggie
 * @version $Revision: 1.5 $ $Date: 2008/10/14 06:47:32 $
 */
public class BaseInfo implements Serializable ,Cloneable{

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	/** path */
	private String path = null;
	private String relPath = null;
	
	private Node node = null;
	private Node rootNode = null;
	private ItemType nodeType = ItemType.NODE;
	
	private String className = null;
	
	public BaseInfo(ItemType nodeType){
		this.nodeType = nodeType;
	}
	
	/**
	 * Return Path
	 * @return
	 */
	public String getPath(){
		return this.path;
	}
	
	private String changetToNpPath(String path)  throws XmlTransactionException{
		if(path == null || path.length() == 0){
			return path;
		}
		String strNp = this.getXmlJcrtInfoDetail().getNamespace();
		if(path.indexOf(strNp+":") != -1){
			return path;
		}
		String resultStr = "";
		if(path.endsWith("/")){
			resultStr = path.substring(1).replaceAll("/", "/"+strNp+":");
		}else{
			resultStr = path.replaceAll("/", "/"+strNp+":");
		}
		if(resultStr.startsWith("/")){
			resultStr = resultStr.substring(1);
		}
		return resultStr;
	}
	/**
	 * Return XML Info
	 * @return
	 * @throws XmlTransactionException
	 */
	public XmlJcrtInfoDetail getXmlJcrtInfoDetail() throws XmlTransactionException{
		XmlServices xmlServices = XmlServices.getInstance();
		return xmlServices.getXmlJcrtInfo().getXmlJcrtInfoDetail(this.getClass());
	}
	/**
	 * Return Class Name
	 * @return class name 
	 */
	public String getClassName(){
		if(this.className == null){
			this.className = this.getClass().getName();
		}
		return this.className;
	}
	
	public void setClassName(String className){
		this.className = className;
	}
	public ItemType getNodeType(){
		return this.nodeType;
	}
	
	public void setPath(String path) throws XmlTransactionException{
		if(path.indexOf("//")!=-1){
			this.path = path.replaceAll("//", "/");
		}else{
			this.path = path;
		}
		if(!this.path.startsWith("/")){
			this.path = "/"+path;
		}
		this.relPath = changetToNpPath(this.path);
	}
	
	public void setRelPath(String relPath)  throws XmlTransactionException{
		this.relPath = changetToNpPath(relPath);
		String strNp = this.getXmlJcrtInfoDetail().getNamespace();
		this.path = this.relPath.replaceAll(strNp+":", "");
	}
	public String getParentPath(){
		String parentPath = this.getPath();
		if((parentPath == null || parentPath.length() == 0) || parentPath == "/"){
			return "/";
		}
		if(parentPath.endsWith("/")){
			parentPath = parentPath.substring(0, parentPath.length() - 1);
		}
		parentPath = parentPath.substring(0, parentPath.lastIndexOf("/"));
		return parentPath;
	}
	public String getRelPath(){
		return this.relPath;
	}

	public Object clone() throws CloneNotSupportedException {
		return super.clone();
		
	}

	public void setRootNode(Node rootNode) {
		this.rootNode = rootNode;
		if(this.rootNode != null){
			try {
				this.node = this.rootNode.getNode(this.getRelPath());
			} catch (PathNotFoundException e) {
				//nothing to do
			} catch (RepositoryException e) {
				//nothing to do
			} catch (Exception e){
				//nothing to do
			}
		}
	}
	
	public Node getRootNode() {
		return rootNode;
	}
	
	public Node getNode() {
		if(this.node == null){
			try {
				this.node = this.rootNode.getNode(this.getRelPath());
			} catch (PathNotFoundException e) {
				//nothing to do
			} catch (RepositoryException e) {
				//nothing to do
			} catch (Exception e){
				//nothing to do
			}
		}
		return node;
	}

	public void setNode(Node node) {
		this.node = node;
	}
	public String toXmlString(){
		StringBuffer sb = new StringBuffer();
		try {
			XmlJcrtInfoDetail xmlDetail = this.getXmlJcrtInfoDetail();
			Iterator iter = xmlDetail.getProperties().keySet().iterator();
			while(iter.hasNext()){
				String proName = (String)iter.next();
				
				sb.append("<"+proName+">");
				Object valueobj = DependHelper.getValue(this, proName);
				if(valueobj != null){
					if(valueobj instanceof Calendar){
						sb.append(UtilHelper.formatDateToString(((Calendar)valueobj).getTime(),""));
					}else{
						sb.append(valueobj.toString());
					}
				}
				sb.append("</"+proName+">");
			}
			sb.append("<path>");
			sb.append(this.getPath());
			sb.append("</path>");
			sb.append("<parentPath>");
			sb.append(this.getParentPath());
			sb.append("</parentPath>");
		} catch (Exception e) {
			// nothing to do
		}
		return sb.toString();
	}
}
